﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZX.HIS.IDAL;
using ZX.HIS.DBUtility;//Please add references
using System.Collections.Generic;
namespace ZX.HIS.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:MedicalItem
    /// </summary>
    public partial class MedicalItem : IMedicalItem
    {
        public MedicalItem()
        { }
        #region Depart自写方法
        /// <summary>
        /// 检查医疗项添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int MedicalItemAdd(ZX.HIS.Model.MedicalItem model)
        {
            string str = "Proc_MedicalItemAdd";
            SqlParameter[] Parameter ={
                                      new SqlParameter("@HospitalId",SqlDbType.Int){Value=model.HospitalId},
                                      new SqlParameter("@MedicalItemName",SqlDbType.VarChar){Value=model.MedicalItemName},
                                      new SqlParameter("@MedicalItemCostPrice",SqlDbType.Decimal){Value= model.MedicalItemCostPrice},
                                      new SqlParameter("@MedicalItemSalePrice",SqlDbType.Decimal){Value= model.MedicalItemSalePrice},
                                      new SqlParameter("@MedicalItemState",SqlDbType.Int){Value= model.MedicalItemState}
                                    };
            object obj = SqlDBhelper.RunProcedureScalar(str, Parameter);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="StrOrder"></param>
        /// <param name="StrWhere"></param>
        /// <returns></returns>
        public List<ZX.HIS.Model.MedicalItem> MedicalItemShow(int PageIndex, int PageSize, string MedicalItemName, int HospitalId)
        {
            StringBuilder str = new StringBuilder();
            str.AppendLine("SELECT * FROM(");
            str.AppendLine("SELECT * FROM(");
            str.AppendLine("SELECT ROW_NUMBER()OVER(");
            str.AppendLine("order by MedicalItemId)as row,tbMedicalItem.* from tbMedicalItem");
            str.AppendLine("WHERE MedicalItemName LIKE '%" + MedicalItemName + "%'");
            str.AppendLine(" AND HospitalId=" + HospitalId);
            str.AppendLine(")TT");
            str.AppendLine("WHERE ROW  between  " + ((PageIndex - 1) * PageSize + 1) + " AND " + PageIndex * PageSize);
            str.AppendLine(")C");
            return SqlDBhelper.GetList<ZX.HIS.Model.MedicalItem>(str.ToString());

        }

        public List<ZX.HIS.Model.MedicalItem> list()
        {
            string SQL = "SELECT * FROM tbMedicalItem";
            return SqlDBhelper.GetList<ZX.HIS.Model.MedicalItem>(SQL);
        }

        /// <summary>
        /// 查询总数量
        /// </summary>
        /// <param name="StrWher"></param>
        /// <returns></returns>
        public int MedicalItemCount(int HospitalId)
        {
            StringBuilder str = new StringBuilder();
            str.Append("SELECT COUNT(*) FROM tbMedicalItem WHERE HospitalId=" + HospitalId);
            return SqlDBhelper.ExecuteScalarInt(str.ToString());
        }

        /// <summary>
        /// 伪删除
        /// </summary>
        /// <param name="IDS"></param>
        /// <param name="DeparStarte"></param>
        /// <returns></returns>
        public int MedicalItemDel(ZX.HIS.Model.MedicalItem mode)
        {
            StringBuilder str = new StringBuilder();
            str.Append("UPDATE tbMedicalItem SET MedicalItemState=" + mode.MedicalItemState + " WHERE MedicalItemId=" + mode.MedicalItemId + " ");
            int i = SqlDBhelper.ExecuteNonQuery(str.ToString());
            return i;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int MedicalItemUpd(ZX.HIS.Model.MedicalItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbMedicalItem set ");
            strSql.Append("MedicalItemName=@MedicalItemName,");
            strSql.Append("MedicalItemCostPrice=@MedicalItemCostPrice,");
            strSql.Append("MedicalItemSalePrice=@MedicalItemSalePrice");
            strSql.Append(" where MedicalItemId=@MedicalItemId");
            SqlParameter[] parameters = {
                    new SqlParameter("@MedicalItemName", SqlDbType.VarChar),
                    new SqlParameter("@MedicalItemCostPrice", SqlDbType.Decimal),
                    new SqlParameter("@MedicalItemSalePrice", SqlDbType.Decimal),
                    new SqlParameter("@MedicalItemId", SqlDbType.Int)};
            parameters[0].Value = model.MedicalItemName;
            parameters[1].Value = model.MedicalItemCostPrice;
            parameters[2].Value = model.MedicalItemSalePrice;
            parameters[3].Value = model.MedicalItemId;
            object obj = SqlDBhelper.ExecuteScalar(strSql.ToString(), parameters);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            else
            {
                return 0;
            }
        }
        #endregion
        #region  BasicMethod

        ///// <summary>
        ///// 得到最大ID
        ///// </summary>
        //public int GetMaxId()
        //{
        //return DbHelperSQL.GetMaxID("MedicalItemId", "tbMedicalItem"); 
        //}

        ///// <summary>
        ///// 是否存在该记录
        ///// </summary>
        //public bool Exists(int MedicalItemId)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) from tbMedicalItem");
        //    strSql.Append(" where MedicalItemId=@MedicalItemId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@MedicalItemId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = MedicalItemId;

        //    return DbHelperSQL.Exists(strSql.ToString(),parameters);
        //}


        ///// <summary>
        ///// 增加一条数据
        ///// </summary>
        //public int Add(ZX.HIS.Model.MedicalItem model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("insert into tbMedicalItem(");
        //    strSql.Append("HospitalId,MedicalItemName,MedicalItemCostPrice,MedicalItemSalePrice,MedicalItemState)");
        //    strSql.Append(" values (");
        //    strSql.Append("@HospitalId,@MedicalItemName,@MedicalItemCostPrice,@MedicalItemSalePrice,@MedicalItemState)");
        //    strSql.Append(";select @@IDENTITY");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@MedicalItemName", SqlDbType.VarChar,50),
        //            new SqlParameter("@MedicalItemCostPrice", SqlDbType.Decimal,9),
        //            new SqlParameter("@MedicalItemSalePrice", SqlDbType.Decimal,9),
        //            new SqlParameter("@MedicalItemState", SqlDbType.Int,4)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.MedicalItemName;
        //    parameters[2].Value = model.MedicalItemCostPrice;
        //    parameters[3].Value = model.MedicalItemSalePrice;
        //    parameters[4].Value = model.MedicalItemState;

        //    object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 更新一条数据
        ///// </summary>
        //public bool Update(ZX.HIS.Model.MedicalItem model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("update tbMedicalItem set ");
        //    strSql.Append("HospitalId=@HospitalId,");
        //    strSql.Append("MedicalItemName=@MedicalItemName,");
        //    strSql.Append("MedicalItemCostPrice=@MedicalItemCostPrice,");
        //    strSql.Append("MedicalItemSalePrice=@MedicalItemSalePrice,");
        //    strSql.Append("MedicalItemState=@MedicalItemState");
        //    strSql.Append(" where MedicalItemId=@MedicalItemId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@MedicalItemName", SqlDbType.VarChar,50),
        //            new SqlParameter("@MedicalItemCostPrice", SqlDbType.Decimal,9),
        //            new SqlParameter("@MedicalItemSalePrice", SqlDbType.Decimal,9),
        //            new SqlParameter("@MedicalItemState", SqlDbType.Int,4),
        //            new SqlParameter("@MedicalItemId", SqlDbType.Int,4)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.MedicalItemName;
        //    parameters[2].Value = model.MedicalItemCostPrice;
        //    parameters[3].Value = model.MedicalItemSalePrice;
        //    parameters[4].Value = model.MedicalItemState;
        //    parameters[5].Value = model.MedicalItemId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        ///// <summary>
        ///// 删除一条数据
        ///// </summary>
        //public bool Delete(int MedicalItemId)
        //{

        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbMedicalItem ");
        //    strSql.Append(" where MedicalItemId=@MedicalItemId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@MedicalItemId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = MedicalItemId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
        ///// <summary>
        ///// 批量删除数据
        ///// </summary>
        //public bool DeleteList(string MedicalItemIdlist )
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbMedicalItem ");
        //    strSql.Append(" where MedicalItemId in ("+MedicalItemIdlist + ")  ");
        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.MedicalItem GetModel(int MedicalItemId)
        //{

        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select  top 1 MedicalItemId,HospitalId,MedicalItemName,MedicalItemCostPrice,MedicalItemSalePrice,MedicalItemState from tbMedicalItem ");
        //    strSql.Append(" where MedicalItemId=@MedicalItemId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@MedicalItemId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = MedicalItemId;

        //    ZX.HIS.Model.MedicalItem model=new ZX.HIS.Model.MedicalItem();
        //    DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
        //    if(ds.Tables[0].Rows.Count>0)
        //    {
        //        return DataRowToModel(ds.Tables[0].Rows[0]);
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.MedicalItem DataRowToModel(DataRow row)
        //{
        //    ZX.HIS.Model.MedicalItem model=new ZX.HIS.Model.MedicalItem();
        //    if (row != null)
        //    {
        //        if(row["MedicalItemId"]!=null && row["MedicalItemId"].ToString()!="")
        //        {
        //            model.MedicalItemId=int.Parse(row["MedicalItemId"].ToString());
        //        }
        //        if(row["HospitalId"]!=null && row["HospitalId"].ToString()!="")
        //        {
        //            model.HospitalId=int.Parse(row["HospitalId"].ToString());
        //        }
        //        if(row["MedicalItemName"]!=null)
        //        {
        //            model.MedicalItemName=row["MedicalItemName"].ToString();
        //        }
        //        if(row["MedicalItemCostPrice"]!=null && row["MedicalItemCostPrice"].ToString()!="")
        //        {
        //            model.MedicalItemCostPrice=decimal.Parse(row["MedicalItemCostPrice"].ToString());
        //        }
        //        if(row["MedicalItemSalePrice"]!=null && row["MedicalItemSalePrice"].ToString()!="")
        //        {
        //            model.MedicalItemSalePrice=decimal.Parse(row["MedicalItemSalePrice"].ToString());
        //        }
        //        if(row["MedicalItemState"]!=null && row["MedicalItemState"].ToString()!="")
        //        {
        //            model.MedicalItemState=int.Parse(row["MedicalItemState"].ToString());
        //        }
        //    }
        //    return model;
        //}

        ///// <summary>
        ///// 获得数据列表
        ///// </summary>
        //public DataSet GetList(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select MedicalItemId,HospitalId,MedicalItemName,MedicalItemCostPrice,MedicalItemSalePrice,MedicalItemState ");
        //    strSql.Append(" FROM tbMedicalItem ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获得前几行数据
        ///// </summary>
        //public DataSet GetList(int Top,string strWhere,string filedOrder)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select ");
        //    if(Top>0)
        //    {
        //        strSql.Append(" top "+Top.ToString());
        //    }
        //    strSql.Append(" MedicalItemId,HospitalId,MedicalItemName,MedicalItemCostPrice,MedicalItemSalePrice,MedicalItemState ");
        //    strSql.Append(" FROM tbMedicalItem ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    strSql.Append(" order by " + filedOrder);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获取记录总数
        ///// </summary>
        //public int GetRecordCount(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) FROM tbMedicalItem ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    object obj = DbHelperSQL.GetSingle(strSql.ToString());
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("SELECT * FROM ( ");
        //    strSql.Append(" SELECT ROW_NUMBER() OVER (");
        //    if (!string.IsNullOrEmpty(orderby.Trim()))
        //    {
        //        strSql.Append("order by T." + orderby );
        //    }
        //    else
        //    {
        //        strSql.Append("order by T.MedicalItemId desc");
        //    }
        //    strSql.Append(")AS Row, T.*  from tbMedicalItem T ");
        //    if (!string.IsNullOrEmpty(strWhere.Trim()))
        //    {
        //        strSql.Append(" WHERE " + strWhere);
        //    }
        //    strSql.Append(" ) TT");
        //    strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///*
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@tblName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@fldName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@PageSize", SqlDbType.Int),
        //            new SqlParameter("@PageIndex", SqlDbType.Int),
        //            new SqlParameter("@IsReCount", SqlDbType.Bit),
        //            new SqlParameter("@OrderType", SqlDbType.Bit),
        //            new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
        //            };
        //    parameters[0].Value = "tbMedicalItem";
        //    parameters[1].Value = "MedicalItemId";
        //    parameters[2].Value = PageSize;
        //    parameters[3].Value = PageIndex;
        //    parameters[4].Value = 0;
        //    parameters[5].Value = 0;
        //    parameters[6].Value = strWhere;	
        //    return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        //}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

